<?php

// 编辑项目
// PUT project.edit

namespace app\api\resource\project;
use think\Db;
use think\Loader;
use think\Session;

class Edit
{
    // 方法
    public $_method = ['PUT'];
    // 前置
    public $_pre    = ['login','isCyz'];
    // 描述
    public $_description = '编辑项目';
    // 参数
    public $_param  = [
        'project_id'   => '项目主键',
        'title'        => '项目名称',
        'words'        => '一句话介绍',
        'logo'         => '项目图标',
        'stage'        => '所处阶段',
        'round'        => '融资轮次',
        'industry'     => '所属行业',
        'city'         => '所在城市',
        'money'        => '融资金额(万)',
        'introduction' => '项目简介',
        'atlas'        => '项目图集,JsonArray',
    ];
    // 返回
    public $_return = [
        201 => '编辑成功',
        400 => '项目不存在',
        422 => '请求参数不合法',
        500 => '服务器错误',
    ];

    public function run(&$request)
    {
        $projectId = $request->put('project_id/d',0,'abs');
        if (0 == $projectId) return [422,'项目主键必须'];

        $ret = Db::name('project')->where('id',$projectId)->field('id')->find();
        if ( is_null($ret) ) return [400,'项目不存在'];

        $memberId               = Session::get('member.id');
        $params['title']        = $request->put('title/s','','trim');
        $params['words']        = $request->put('words/s','','trim');
        $params['logo']         = $request->put('logo/s','','trim');
        $params['stage']        = $request->put('stage/s','','trim');
        $params['round']        = $request->put('round/s','','trim');
        $params['industry']     = $request->put('industry/s','','trim');
        $params['city']         = $request->put('city/s','','trim');
        $params['money']        = $request->put('money/s','','trim');
        $params['introduction'] = $request->put('introduction/s','','trim');
        $params['atlas']        = $request->put('atlas/s','','trim');

        $validate = Loader::validate('Project');
        if ( !$validate->scene('edit')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $params['update_time'] = time();
        $num = Db::name('project')->where('id',$projectId)->where('member_id',$memberId)->update($params);

        return ($num >= 0) ? [ 201,[] ] : 500;
    }
}